var copyfiles = require('copyfiles')
var path = require('path')
var shell = require('shelljs')
var ora = require('ora')

function depth(string) {
  return path.normalize(string).split(path.sep).length - 1;
}

function copy (src, dest) {
  var dist = path.join(src, 'dist')
  return new Promise(function (resolve, reject) {
    copyfiles([dist + '/**/*', dest], { up: depth(dist) + 1, soft: false }, function (err) {
      if (err) {
        return reject(err)
      }
      resolve()
    })
  })
}

module.exports = function (src, dest) {
  var spinner = ora('installing node modules')
  spinner.start()
  shell.cd(src)
  shell.exec('npm install')
  spinner.stop()

  shell.exec('npm run build')
  return copy(src, dest)
}
